#coding:utf8
import pickle
from nltk.collocations import BigramCollocationFinder
from nltk.metrics.association import BigramAssocMeasures
import os


def use_classifier(filename):
    def best_word_features(words):  
        return dict([(word, True) for word in words if word in best_words])
    
    def best_bigram_features(words, score_fn=BigramAssocMeasures.chi_sq, n=1000):
        bigram_finder = BigramCollocationFinder.from_words(words)  
        bigrams = bigram_finder.nbest(score_fn, n) 
        return best_word_features(bigrams)
    
    def best_word_bigram_features(words, score_fn=BigramAssocMeasures.chi_sq, n=1000):
        bigram_finder = BigramCollocationFinder.from_words(words)
        bigrams = bigram_finder.nbest(score_fn, n)
        bg_wd = words + bigrams
        return best_word_features(bg_wd)
    
    clf = pickle.load(open(os.path.dirname(os.path.dirname(os.getcwd()))+'\\data\\classifier.pkl','rb')) #载入分类器


    best_words = clf[0]
    
    
    moto = pickle.load(open(filename,'rb')) #载入文本数据 pkl
       
       
    def extract_features(data):
        feat = []
        for i in data:
            feat.append(best_word_bigram_features(i))
        return feat
       
    moto_features = extract_features(moto) #把文本转化为特征表示的形式
       
     
      
    pred = clf[1].prob_classify_many(moto_features) #该方法是计算分类概率值的
    p_file = open(os.path.dirname(os.path.dirname(os.getcwd()))+'\\data\\score.txt','w') #把结果写入文档
    for i in pred:
        p_file.write(str(i.prob('pos')) + ' ' + str(i.prob('neg')) + '\n')
    p_file.close()
    
